<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Report_model extends MY_Model
{
	private $_host;
	private $_dbname;
	private $_username;
	private $_password;
	private $_port;
	private $_folderImageStore = '../uploads/images/';
	private $_folderImageStoreUrl = '/uploads/images/';
	
	function Report_model()
	{
		parent::MY_Model();	
        $host = $this->db->hostname;
		$arrHost = explode(':',$host);
		$temp =$this->_host = trim($arrHost[0]);
		$this->_dbname = $this->db->database;
		$this->_username = $this->db->username;
		$this->_password = $this->db->password;
		$this->_port = $this->db->port;
	}
	function selfserving_report_summary($fromdate,$todate){
		$data = array();
        $data['data1']=array();
		$data['data2']=array();
		
		mysqli_multi_query($this->db->conn_id, "CALL selfserving_report_summary('$fromdate','$todate');") or die(mysqli_error($iconn));
		
        $result = mysqli_store_result($this->db->conn_id);
        while($row = mysqli_fetch_array($result))
		{
			
			$data['data1'][] = $row;
        }
        mysqli_free_result($result);
		if(mysqli_more_results($this->db->conn_id))
		{
			mysqli_next_result($this->db->conn_id);
		}
		return $data;
		
	}
	function selfserving_get_group_chart($fromdate,$todate,$lstgroupid)
    {
		$data = array();
        $data['data1'] =array();
        $data['data2'] =array();
        
		mysqli_multi_query($this->db->conn_id, "CALL selfserving_get_group_chart('$fromdate','$todate','$lstgroupid');") or die(mysqli_error($iconn));
        $result = mysqli_store_result($this->db->conn_id);
        while($row = mysqli_fetch_array($result))
		{	
			$data['data1'][] = $row;
        }
        mysqli_free_result($result);
		mysqli_next_result($this->db->conn_id);
        $result = mysqli_store_result($this->db->conn_id);
        while($row1 = mysqli_fetch_array($result))
		{
			$data['data2'][] = $row1;
		}
		mysqli_free_result($result);
		if(mysqli_more_results($this->db->conn_id))
		{
			mysqli_next_result($this->db->conn_id);
		}
		return $data;
	}
    function selfserving_get_all_agency()
    {
		$data = array();
		mysqli_multi_query($this->db->conn_id, "CALL selfserving_get_all_agency();") or die(mysqli_error($iconn));
        $result = mysqli_store_result($this->db->conn_id);
        while($row = mysqli_fetch_array($result))
		{
			$data[] = $row;
        }
		mysqli_free_result($result);
		if(mysqli_more_results($this->db->conn_id))
		{
			mysqli_next_result($this->db->conn_id);
		}
		return $data;
	}
    function ssv_report_by_site($fromdate,$todate)
    {
		$data = array();
		mysqli_multi_query($this->db->conn_id, "CALL ssv_report_by_site('$fromdate','$todate');") or die(mysqli_error($iconn));
        $result = mysqli_store_result($this->db->conn_id);
        while($row = mysqli_fetch_array($result))
		{
			
			$data[] = $row;
        }
		mysqli_free_result($result);
		if(mysqli_more_results($this->db->conn_id))
		{
			mysqli_next_result($this->db->conn_id);
		}
		return $data;
	}
	
	function get_report_by_group($fromdate, $todate)
	{
		return $this->getRows("CALL selfserving_report_bygroup('$fromdate', '$todate');");
	}
	
	function get_report_by_group_bydate($fromdate, $todate, $type = -1)
	{
		return $this->getRows("CALL selfserving_report_bygroup_bydate('$fromdate', '$todate', $type);", true, 'object');
	}
	
	function get_report_by_group_byhour($date)
	{
		return $this->getRows("CALL selfserving_admin_get_chart_hour('$date');", true, 'object');
	}
	
	function ssv_report_by_site_v2($fromdate, $todate, $domain = '')
	{
		return $this->getRows("CALL ssv_report_by_site_v3('$fromdate', '$todate', '$domain');", true, 'object');
	}
	
	function get_report_financial($uid, $permitid, $type, $startdate, $enddate, $page = 1, $numrow = 2000 )
	{
		$result = $this->getRowsOnMultiQuery("CALL selfserving_report_financial_admin_public($uid,$permitid,$type,'$startdate','$enddate',$page,$numrow);", 'object');
		if($type == 1)	//get list by noi bo, khach hang
		{
			$out = new stdClass();
			$out->items = $result[0];
			$out->pages = count($result[1]) > 0 ? $result[1][0] : $result[1];
			$out->summary = count($result[2]) > 0 ? $result[2][0] : $result[2];
			return $out;
		}
		else if($type == 0) return $result[0];
	}
	
	function get_report_financial_agency($uid, $permitid, $groupid, $startdate, $enddate, $page = 1, $numrow = 20 )
	{
		$out = new stdClass();
		$result = $this->getRowsOnMultiQuery("CALL selfserving_report_financial_agency_admin_public($uid,$permitid,$groupid,'$startdate','$enddate',$page,$numrow);", 'object');
		$out->items = $result[0];
		$out->pages = count($result[1]) > 0 ? $result[1][0] : $result[1];
		$out->summary = count($result[2]) > 0 ? $result[2][0] : $result[2];
		return $out;
	}
	
	function getGroupInfo($gid)
	{
		return $this->getRows("CALL selfserving_group_info($gid);", false, 'object');
	}
	
	function get_sale_earn($startdate, $enddate, $groupid = -1, $typead = -1)
	{
		return $this->getRows("CALL selfserving_admin_report_sale_earn_summary('$startdate', '$enddate', $groupid, $typead);", true, 'object');
	}
	
	function get_sale_earn_detail($saleid, $startdate, $enddate, $typead = -1)
	{
		return $this->getRows("CALL selfserving_admin_report_sale_earn_detail($saleid, '$startdate', '$enddate', $typead);", true, 'object');
	}
	
	function get_sale_earn_customer($saleid, $customerid, $startdate, $enddate, $typead = -1)
	{
		return $this->getRows("CALL selfserving_admin_report_sale_earn_detail_by_customer($saleid, $customerid, '$startdate', '$enddate', $typead);", true, 'object');
	}
	
	function selfserving_report_banner_tracking($page = 1, $limit = 20)
	{
		$result = $this->getRowsOnMultiQuery("CALL selfserving_report_banner_tracking($page, $limit);", 'object');
		$data = array();
		$data['items'] = $result[0];
		$data['pages'] = (int)$result[1][0]->totalpage;
		return $data;
	}
	
	function get_group_by_hour($gid, $date)
	{
		return $this->getRows("CALL selfserving_admin_get_chart_hour_by_groupid($gid, '$date')", true, 'object');
	}
    function selfserving_get_banner_defaul($page,$rownum)
    {
        $CI = &get_instance();
        $CI->load->library('mycommon');
		
        $data=array();
        $data['banner']=array();
        $data['total']=array();
        $iconn = $this->db->conn_id;
        mysqli_multi_query($iconn, "CALL selfserving_get_banner_defaul('$page','$rownum');") or show_custom_error(mysqli_error($iconn));		
        $result = mysqli_store_result($iconn);
        while($row = mysqli_fetch_array($result))
		{
            $row['filepath'] = '';
			$createdTime = date($row['createtime']);
			$createdTimeInfo = getdate(strtotime($createdTime));
			$yearFolder = $createdTimeInfo['year'];
			$monthFolder = $createdTimeInfo['mon'];
			$monthFolder = ($monthFolder < 10) ? "0" . $monthFolder : $monthFolder;
			$row['filepath'] = $CI->mycommon->getBannerImagPath($yearFolder, $monthFolder, $row['filename']);
			$data['banner'][] = $row;
		}
        mysqli_free_result($result);
		mysqli_next_result($iconn);
		
        $result = mysqli_store_result($iconn);
        while($row1 = mysqli_fetch_array($result))
		{
			$data['totalpage'] = $row1;
		}
        mysqli_free_result($result);
		if(mysqli_more_results($iconn))
		{
			mysqli_next_result($iconn);
		}
		return $data;
	}
	
	function get_domain_report_config_info($cfid)
	{
		$data = array();
		$iconn = $this->db->conn_id;
		mysqli_multi_query($iconn, "CALL selfserving_admin_set_config_domain_email('3', '$cfid', '', '', '', '');") or die(mysqli_error($iconn));
		$result = mysqli_store_result($iconn);
		$data = mysqli_fetch_array($result);
		
		mysqli_free_result($result);
		if(mysqli_more_results($iconn))
		{
			mysqli_next_result($iconn);
		}
		
		return $data;
	}
	
	function config_domain_email_set($cfid, $strDomain, $fullname, $email, $mess)
	{
		
		$iconn = $this->db->conn_id;
		mysqli_multi_query($iconn, "CALL selfserving_admin_set_config_domain_email('1', '$cfid', '$strDomain', '$fullname', '$email', '$mess');") or die(mysqli_error($iconn));
		if(mysqli_more_results($iconn))
		{
			mysqli_next_result($iconn);
		}
	}
	function delete_config_domain_email($id)
	{
		
		$iconn = $this->db->conn_id;
		mysqli_multi_query($iconn, "CALL selfserving_admin_set_config_domain_email('4', '$id', '', '', '', '');") or die(mysqli_error($iconn));
		if(mysqli_more_results($iconn))
		{
			mysqli_next_result($iconn);
		}
	}
	
	function config_domain_email_get_list_user()
	{
		$data = array();
		$iconn = $this->db->conn_id;
		mysqli_multi_query($iconn, "CALL selfserving_admin_config_domain_email_get_user();") or die(mysqli_error($iconn));
		$result = mysqli_store_result($iconn);
		while($row = mysqli_fetch_array($result))
		{
			$data[] = $row;
		}
		mysqli_free_result($result);
		if(mysqli_more_results($iconn))
		{
			mysqli_next_result($iconn);
		}
		return $data;
	}
	/* cpc domain report */
	function cpc_domain_report($type, $fromdate, $todate, $strGroupCrc)
	{
		$data = array();
  		$iconn = $this->db->conn_id;
		mysqli_multi_query($iconn, "CALL selfserving_admin_domain_report('$fromdate', '$todate', '$type', '$strGroupCrc');")  or die(show_custom_error(mysqli_error($iconn)));
		$result = mysqli_store_result($iconn);
		while($row = mysqli_fetch_array($result))
		{
			$data[] = $row;
		}
		mysqli_free_result($result);
		if(mysqli_more_results($iconn))
		{
			mysqli_next_result($iconn);
		}
		return $data;
	}
	/* end cpc domain report */
    function camp_report_bydomain($campid, $fromdate, $todate, $strDomainCrc, $type)
	{
		$data = array();
  		$iconn = $this->db->conn_id;
		mysqli_multi_query($iconn, "CALL selfserving_admin_camp_report_bydomain('$campid', '$fromdate', '$todate', '$strDomainCrc', '$type');")  or die(show_custom_error(mysqli_error($iconn)));
		$result = mysqli_store_result($iconn);
		while($row = mysqli_fetch_array($result))
		{
			$data[] = $row;
		}
		mysqli_free_result($result);
		if(mysqli_more_results($iconn))
		{
			mysqli_next_result($iconn);
		}
		return $data;
	}
}
?>